package com.ruoyi.contract.service.impl;

import com.ruoyi.base.util.GenCodeUtils;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.contract.domain.ContractInfo;
import com.ruoyi.contract.domain.ContractPlanInfo;
import com.ruoyi.contract.mapper.ContractInfoMapper;
import com.ruoyi.contract.mapper.ContractPlanInfoMapper;
import com.ruoyi.contract.service.IContractInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 合同信息Service业务层处理
 *
 * @author ruoyi
 * @date 2022-08-31
 */
@Service
public class ContractInfoServiceImpl implements IContractInfoService {
    @Autowired
    private ContractInfoMapper contractInfoMapper;

    @Autowired
    private ContractPlanInfoMapper contractPlanInfoMapper;

    /**
     * 查询合同信息
     *
     * @param id 合同信息主键
     * @return 合同信息
     */
    @Override
    public ContractInfo selectContractInfoById(Long id) {
        ContractInfo contractInfo = contractInfoMapper.selectContractInfoById(id);
        ContractPlanInfo contractPlanInfo = new ContractPlanInfo();
        contractPlanInfo.setContractNo(contractInfo.getContractNo());
        List<ContractPlanInfo> contractPlanInfos = contractPlanInfoMapper.selectContractPlanInfoList(contractPlanInfo);
        contractInfo.setContractPlanInfoList(contractPlanInfos);
        return contractInfo;
    }

    /**
     * 查询合同信息列表
     *
     * @param contractInfo 合同信息
     * @return 合同信息
     */
    @Override
    public List<ContractInfo> selectContractInfoList(ContractInfo contractInfo) {
        return contractInfoMapper.selectContractInfoList(contractInfo);
    }

    /**
     * 新增合同信息
     *
     * @param contractInfo 合同信息
     * @return 结果
     */
    @Transactional(rollbackFor = {Exception.class,ServiceException.class})
    @Override
    public int insertContractInfo(ContractInfo contractInfo) {
        contractInfo.setCreateTime(DateUtils.getNowDate());
        SysUser user = SecurityUtils.getLoginUser().getUser();
        contractInfo.setCreateBy(user.getUserName());
        contractInfo.setContractNo(GenCodeUtils.getContractNo());
        //预付款,合同就是进行中
        if ("1".equals(contractInfo.getIsPay())){
            contractInfo.setStatus("1");
        }else {
            contractInfo.setStatus("0");
        }
        //合同编号定金万元化成分
        try{
            contractInfo.setPayMount(contractInfo.getPayMount().multiply(BigDecimal.valueOf(1000000)));
        }catch (Exception e){
            throw new ServiceException("合同金额不正确!");
        }
        int rows = contractInfoMapper.insertContractInfo(contractInfo);
        insertContractPlanInfo(contractInfo);
        return rows;
    }

    /**
     * 修改合同信息
     *
     * @param contractInfo 合同信息
     * @return 结果
     */
    @Transactional(rollbackFor = {Exception.class,ServiceException.class})
    @Override
    public int updateContractInfo(ContractInfo contractInfo) {
        contractInfo.setUpdateTime(DateUtils.getNowDate());
        SysUser user = SecurityUtils.getLoginUser().getUser();
        contractInfo.setUpdateBy(user.getUserName());
        //预付款,合同就是进行中
        if ("0".equals(contractInfo.getIsPay())){
            contractInfo.setStatus("1");
        }else {
            contractInfo.setStatus("0");
        }
        //合同编号定金万元化成分
        if (contractInfo.getPayMount() != null){
            try{
                contractInfo.setPayMount(contractInfo.getPayMount().multiply(BigDecimal.valueOf(1000000)));
            }catch (Exception e){
                throw new ServiceException("合同金额不正确!");
            }
        }
        contractInfoMapper.deleteContractPlanInfoByContractNo(contractInfo.getContractNo());
        insertContractPlanInfo(contractInfo);
        return contractInfoMapper.updateContractInfo(contractInfo);
    }

    /**
     * 批量删除合同信息
     *
     * @param ids 需要删除的合同信息主键
     * @return 结果
     */
    @Transactional(rollbackFor = {Exception.class,ServiceException.class})
    @Override
    public int deleteContractInfoByIds(Long[] ids) {
        for (Long id : ids) {
            ContractInfo contractInfo = contractInfoMapper.selectContractInfoById(id);
            if (StringUtils.isEmpty(contractInfo.getContractNo())) {
                continue;
            }
            contractInfoMapper.deleteContractPlanInfoByContractNo(contractInfo.getContractNo());
        }
        return contractInfoMapper.deleteContractInfoByIds(ids);
    }

    /**
     * 删除合同信息信息
     *
     * @param id 合同信息主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteContractInfoById(Long id) {
        ContractInfo contractInfo = contractInfoMapper.selectContractInfoById(id);
        contractInfoMapper.deleteContractPlanInfoByContractNo(contractInfo.getContractNo());
        return contractInfoMapper.deleteContractInfoById(id);
    }

    /**
     * 新增合同计划信息
     *
     * @param contractInfo 合同信息对象
     */
    public void insertContractPlanInfo(ContractInfo contractInfo) {
        List<ContractPlanInfo> contractPlanInfoList = contractInfo.getContractPlanInfoList();
        String contractNo = contractInfo.getContractNo();
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (StringUtils.isNotNull(contractPlanInfoList)) {
            List<ContractPlanInfo> list = new ArrayList<ContractPlanInfo>();
            for (ContractPlanInfo contractPlanInfo : contractPlanInfoList) {
                contractPlanInfo.setContractNo(contractNo);
                contractPlanInfo.setCreateBy(user.getCreateBy());
                contractPlanInfo.setCreateTime(DateUtils.getNowDate());
                list.add(contractPlanInfo);
            }
            if (list.size() > 0) {
                contractInfoMapper.batchContractPlanInfo(list);
            }
        }
    }
}
